package com.cnpinxi.controller.admin.messageBoard;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.MessageBoard;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.MessageBoardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/admin/messageBoard")
@Controller
public class AdminMessageBoardController extends BaseController {

@Autowired
MessageBoardService messageBoardService;
    /**
     * 编辑产品名称
     * @param tid
     * @param status
     * @return
     */
    @RequestMapping(value = "/modifyMessageBoardStatus")
    @ResponseBody
    public String modifyMessageBoardStatus(long tid,Byte status){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            messageBoardService.modifyMessageBoardStatus(tid,status);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    @RequestMapping("/messageBoardList")
    public ModelAndView messageBoardList(HttpServletRequest request,Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/messageBoard/messageBoardList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));

        PageHelper.startPage(page, pageSize);
        List<MessageBoard> messageBoardList = messageBoardService.getMessageBoardList(map);
        PageInfo<MessageBoard> pageinfo = new PageInfo<>(messageBoardList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }
}
